<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>安装新插件</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="//lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../js/lay-module/step-lay/step.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-card-body" style="padding-top: 40px;">
                    <div class="layui-carousel" id="stepForm" lay-filter="stepForm" style="margin: 0 auto;">
                        <div carousel-item>
                            <div>
                                <form class="layui-form"
                                      style="margin: 0 auto;max-width: 460px;padding-top: 40px;text-align: center">
                                    <div class="layui-form-item">
                                        <div class="layui-upload-drag" id="plugin-upload" style="margin: auto">
                                            <i class="layui-icon layui-icon-upload"></i>
                                            <p>点击上传，或将文件拖拽到此处</p>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div>
                                <form class="layui-form" style="margin: 0 auto;max-width: 460px;padding-top: 40px;"
                                      lay-filter="plugin-info">
                                    <div class="layui-form-item" style="display: none">
                                        <label class="layui-form-label">id:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="id" placeholder="请填写入插件id" class="layui-input"
                                                   required disabled/>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">插件分类:</label>
                                        <div class="layui-input-block">
                                            <select lay-verify="required" name="category_id" id="categories">
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">插件标题:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="title" placeholder="填写插件的标题"  lay-verify="required" lay-reqtext="插件标题不能为空" class="layui-input"
                                                   required/>
                                            <tip>填写插件的标题。</tip>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">路由别名:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="alias" placeholder="请输入路由别名" value=""
                                                   class="layui-input" required>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">插件类名:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="class" placeholder="请填写插件类名" value=""
                                                   class="layui-input" required disabled>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">插件描述:</label>
                                        <div class="layui-input-block">
                                            <textarea name="desc" placeholder="插件描述" value=""
                                                      class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <button type="button" class="layui-btn layui-btn-primary pre">上一步</button>
                                            <button class="layui-btn" lay-submit lay-filter="formStep2">
                                                &emsp;确认安装&emsp;
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div>
                                <div style="text-align: center;margin-top: 90px;">
                                    <i class="layui-icon layui-circle"
                                       style="color: white;font-size:30px;font-weight:bold;background: #52C41A;padding: 20px;line-height: 80px;">&#xe605;</i>
                                    <div style="font-size: 24px;color: #333;font-weight: 500;margin-top: 30px;">
                                        安装成功
                                    </div>
                                    <div style="font-size: 14px;color: #666;margin-top: 20px;">哎呦，你很不错哦</div>
                                </div>
                                <div style="text-align: center;margin-top: 50px;">
                                    <button class="layui-btn next">再安装一个</button>
                                    <a id="viewPlugin" href="/" target="_blank"
                                       class="layui-btn layui-btn-primary">查看插件</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <div style="color: #666;margin-top: 30px;margin-bottom: 40px;padding-left: 30px;">
                        <h3>说明</h3><br>
                        <h4>安装新插件</h4>
                        <p>上传符合插件格式的zip压缩包</p>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script id="categoies_tpl" type="text/html">
    <select lay-verify="required" name="category_id">
        <option value="0">默认组</option>
        {{# layui.each(d, function(index, item){ }}
        <option value="{{ item.id }}">{{ item.title }}</option>
        {{# }); }}
    </select>
</script>
<script src="//lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../js/api.js" charset="utf-8"></script>
<script src="../../js/common.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'step'], function () {
        var $ = layui.$,
            form = layui.form,
            step = layui.step,
            upload = layui.upload,
            layer = layui.layer,
            laytpl = layui.laytpl;

        httpGet('/admin/category/list').then(res => {
            var getTpl = document.getElementById('categoies_tpl').innerHTML,
                view = document.getElementById('categories');
            laytpl(getTpl).render(res.data.items, function (html) {
                view.innerHTML = html;
            });
        })
        step.render({
            elem: '#stepForm',
            filter: 'stepForm',
            width: '100%', //设置容器宽度
            stepWidth: '750px',
            height: '700px',
            stepItems: [{
                title: '上传插件包'
            }, {
                title: '填写插件信息'
            }, {
                title: '完成'
            }]
        });
        //拖拽上传
        upload.render({
            elem: '#plugin-upload'
            , url: '/admin/plugin/upload'
            , accept: 'file'
            , acceptMime: 'application/zip'
            , before: function (obj) {
                layer.load(1);
            }
            , done: function (res) {
                if (res.status === 'ok') {
                    $message.success('上传成功');
                    form.val("plugin-info", res.data);
                    step.next('#stepForm');

                } else {
                    $message.error(res.message);
                }
                layer.closeAll('loading');
            }, error: function () {
                $message.error('上传失败');
                layer.closeAll('loading');
            }
        });
        form.on('submit(formStep)', function (data) {
            step.next('#stepForm');
            return false;
        });

        form.on('submit(formStep2)', function (data) {
            layer.load(1);
            let pluginInfo = form.val("plugin-info");
            httpPost('/admin/plugin/edit', pluginInfo).then(res => {
                if (res.status === 'ok') {
                    step.next('#stepForm');
                    $('#viewPlugin').attr('href', '/' + pluginInfo.alias)
                }
            }).finally(() => {
                layer.closeAll('loading')
            })
            return false;
        });

        $('.pre').click(function () {
            step.pre('#stepForm');
        });

        $('.next').click(function () {
            step.next('#stepForm');
        });
    })
</script>
</body>
</html>